from selenium.webdriver.common.by import By

from base.ObjectMap import ObjectMap
from base.TerminalXpath import TerminalBase


class TerminalPage(TerminalBase, ObjectMap):
    def common_click_button(self, driver, button_name):
        button_xpath = self.common_button(button_name)
        return self.element_click(driver, By.XPATH, button_xpath)

    def common_input_value(self, driver, label_name, input_value):
        input_xpath = self.label_input(label_name)
        return self.element_fill_value(driver, By.XPATH, input_xpath, input_value)

    def click_combobox(self, driver, label_name):
        div_xpath = self.combobox(label_name)
        return self.element_click(driver, By.XPATH, div_xpath)

    def select_detail_combobox(self, driver, item):
        item_xpath = self.combobox_detail(item)
        return self.element_click(driver, By.XPATH, item_xpath)

    def uncheck(self, driver, target):
        hidden_xpath = self.hidden_checkbox(target)
        if self.element_get(driver, By.XPATH, hidden_xpath).is_selected():
            displayed_xpath = self.displayed_checkbox(target)
            self.element_click(driver, By.XPATH, displayed_xpath)

    def check(self, driver, target):
        hidden_xpath = self.hidden_checkbox(target)
        if not self.element_get(driver, By.XPATH, hidden_xpath).is_selected():
            displayed_xpath = self.displayed_checkbox(target)
            self.element_click(driver, By.XPATH, displayed_xpath)

    def get_alert(self, driver):
        alert_xpath = self.alert_message()
        text = self.element_get(driver, By.XPATH, alert_xpath).text
        close_xpath = self.alert_close()
        self.element_click(driver, By.XPATH, close_xpath)
        return text

    def common_click_close(self, driver):
        close_css = self.common_close()
        return self.element_click(driver, By.XPATH, close_css)

    def does_direct_terminal_exist(self, driver, terminal_name, merchant_name):
        self.uncheck(driver, "includeSubEnterprise")
        self.common_click_button(driver, "Filter")
        self.common_input_value(driver, "Terminal Name", terminal_name)
        self.common_click_button(driver, "Search")
        name_xpath = self.terminal_name(terminal_name, merchant_name)
        return self.element_is_display(driver, By.XPATH, name_xpath)

    def create_terminal(self, driver, terminal_name, merchant_name, model_name):
        if self.does_direct_terminal_exist(driver, terminal_name, merchant_name):
            return True
        self.common_click_button(driver, "Create")
        self.click_combobox(driver, "Merchant")
        self.select_detail_combobox(driver, merchant_name)
        self.click_combobox(driver, "Model")
        self.select_detail_combobox(driver, model_name)
        self.common_input_value(driver, "Terminal Name", terminal_name)
        self.common_click_button(driver, "Confirm")
        return True

    def click_terminal_action(self, driver, terminal_name):
        button_xpath = self.terminal_action(terminal_name)
        self.element_click(driver, By.XPATH, button_xpath)

    def click_terminal_detail(self, driver, terminal_name):
        detail_xpath = self.terminal_detail(terminal_name)
        self.element_click(driver, By.XPATH, detail_xpath)

    def click_action_detail(self, driver, terminal_name, action_name):
        detail_xpath = self.action_detail(terminal_name, action_name)
        self.element_click(driver, By.XPATH, detail_xpath)

    def click_publish(self, driver, app_name, role):
        publish_xpath = self.publish_button(app_name, role)
        self.element_click(driver, By.XPATH, publish_xpath)

    def push_application(self, driver, terminal_name, app_name):
        self.click_terminal_action(driver, terminal_name)
        self.click_terminal_detail(driver, terminal_name)
        self.common_click_button(driver, "Application")
        self.common_click_button(driver, "Push Application")
        self.click_publish(driver, app_name, "Push Application")
        self.common_click_button(driver, "Confirm")
        return True

    def delete_terminal(self, driver, terminal_name):
        self.click_terminal_action(driver, terminal_name)
        self.click_action_detail(driver, terminal_name, "Remove")
        self.common_click_button(driver, "Confirm")
        return True
